//
//  SISSContractRightFillInCell.m
//  SISSAPP
//
//  Created by 优房网 on 2017/8/16.
//  Copyright © 2017年 优房网. All rights reserved.
//

#import "SISSContractRightFillInCell.h"

@interface SISSContractRightFillInCell ()<UITextFieldDelegate>

/// Label
@property (weak, nonatomic) UILabel *label;
/// textField
@property (weak, nonatomic) UITextField *textField;
/* bottomLine */
@property (weak, nonatomic) UIView *bottomLine;


@end
@implementation SISSContractRightFillInCell


- (void)setContractModel:(SISSContractModel *)contractModel {
    [super setContractModel:contractModel];
    
    if (contractModel.leftStr.length > 0 && [[contractModel.leftStr substringToIndex:1] isEqualToString:@"*"]) {
        NSMutableAttributedString *attributedStr = [[NSMutableAttributedString alloc] initWithString:contractModel.leftStr];
        [attributedStr addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0,1)];
        self.label.attributedText = attributedStr;
    } else {
        self.label.text = contractModel.leftStr;
    }
    
    self.textField.keyboardType = [SISSPublicMethods matchKeybordTypeWithKeyboardNum:contractModel.keyboardNum];
    
    self.textField.placeholder = contractModel.placeHolder;
    self.textField.text = contractModel.rightStr;
}

#pragma mark - UITextFieldDelegate
- (void)textFieldDidEndEditing:(UITextField *)textField {
    
    self.contractModel.rightStr = textField.text;
    
    // 名字
    if ([self.contractModel.leftStr containsString:@"姓名："] && textField.text.length < 2) {
        
        [SISSTools showSVTitle:@"姓名不能少于2个字" andTime:1 andColor:YES];
    }
}

//- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
//
//    if (textField == self.textField && self.contractModel.firstLimitLength > 0) {
//        NSString *str = [NSString stringWithFormat:@"%@%@",textField.text,string];
//        if (str.length > self.contractModel.firstLimitLength) {
//            [SISSTools showSVTitle:self.contractModel.firstLimitTipStr andTime:1 andColor:YES];
//            return NO;
//        }
//    }
//
//    return YES;
//}

//实现方法
- (void)textFieldDidChange:(UITextField *)textField {
    if (self.contractModel.firstLimitLength && textField.text.length > self.contractModel.firstLimitLength) {
        //提示语
        [SISSTools showSVTitle:self.contractModel.firstLimitTipStr andTime:1 andColor:YES];
        //截取
        textField.text = [textField.text substringToIndex:self.contractModel.firstLimitLength];
    }
}

#pragma mark - UI
- (void)initUI {
    [super initUI];
    
    UILabel *label = [[UILabel alloc] init];
    label.numberOfLines = 0;
    label.textColor = MAIN_TEXT_COLOR;
    label.font = [UIFont systemFontOfSize:16];
    label.textAlignment = NSTextAlignmentRight;
    [self.contentView addSubview:label];
    self.label = label;
    
    UITextField *textField = [[UITextField alloc] init];
    textField.delegate = self;
    textField.textColor = MAIN_TEXT_COLOR;
    textField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@" " attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:16],NSForegroundColorAttributeName:SECONDARY_TEXT_COLOR}];
    [self.contentView addSubview:textField];
    self.textField = textField;
    //添加事件
    [self.textField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
   
    UIView *bottomLine = [[UIView alloc] init];
    bottomLine.backgroundColor = [UIColor groupTableViewBackgroundColor];
    [self.contentView addSubview:bottomLine];
    self.bottomLine = bottomLine;
}

#pragma mark - 约束
- (void)initConstraint {
    [super initConstraint];
    
    WeakSelf
    CGFloat margin = 8.0;
    [self.label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.equalTo(weakSelf.contentView).offset(1.5 * margin);
    }];
    
    [self.textField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.contentView).offset(20 * margin);
        make.centerY.equalTo(weakSelf.contentView);
        make.right.equalTo(weakSelf.contentView).offset(-margin);
    }];
    
    [self.bottomLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.label.mas_bottom).offset(1.5 * margin);
        make.left.right.bottom.equalTo(weakSelf.contentView);
        make.height.mas_equalTo(1);
    }];
}


@end
